package com.oasystem.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.oasystem.dao.jdbc.ProfitDAO;
import com.oasystem.domain.Member;
import com.oasystem.domain.MemberProfitSearchResult;
import com.oasystem.domain.PagingBean;
import com.oasystem.domain.ProfitDetail;
import com.oasystem.domain.ProfitDetailGbptBean;
import com.oasystem.domain.ProfitRecord;
import com.oasystem.domain.ProfitReturnAmountHistory;
import com.oasystem.domain.ProfitReturnedRecord;
import com.oasystem.domain.ProfitSearchResult;
import com.oasystem.domain.ProfitSummary;
import com.oasystem.utils.DateUtils;
import com.oasystem.utils.StringUtils;

@Transactional(readOnly = true)
public class ProfitService {
	private ProfitDAO profitDAO;

	public void setProfitDAO(ProfitDAO profitDAO) {
		this.profitDAO = profitDAO;
	}

	public int retrieveShouldReturnProfitMemberCount(String countDate) {
		return profitDAO.retrieveShouldReturnProfitMemberCount(countDate);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void generateProfitDetails(String countDate,
			ProfitRecord profitRecord, PagingBean<ProfitDetail> pagingBean) {
		profitDAO.insertProfitRecord(profitRecord,
				StringUtils.removeSpace(countDate));
		profitDAO.updateTodayShouldReturnAmounts(Math.round((profitRecord
				.getTotalReturnAmount() / profitRecord
				.getAcceptReturnMemberCount()) * 100) / 100.00);
		profitDAO.retrieveProfitDetails(pagingBean,
				StringUtils.removeSpace(countDate));
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void updateReturnProfitAmount(String memberIDs, double newAmount) {
		profitDAO.updateReturnProfitAmount(memberIDs, newAmount);
	}

	public void pagingList(PagingBean<ProfitDetail> pagingBean, String countDate) {
		profitDAO.retrieveProfitDetails(pagingBean, countDate);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void batchReturnProfit(String countDate, String memberIDs,
			String profitID) {
		profitDAO.batchReturnProfit(countDate, memberIDs, profitID);
	}

	public ProfitDetailGbptBean retrieveProfitReturnRecordToday() {
		ProfitDetailGbptBean bean = new ProfitDetailGbptBean();
		bean.setProfitReturnedRecordList(profitDAO
				.retrieveProfitReturnRecordToday());
		BigDecimal sum = new BigDecimal(0);
		for (ProfitReturnedRecord detail : bean.getProfitReturnedRecordList()) {
			BigDecimal amount = new BigDecimal(detail.getTodayReturnAmount());
			BigDecimal amountFen = amount.multiply(new BigDecimal(100));
			detail.setTodayReturnAmount(amountFen.setScale(0, RoundingMode.UP)
					.toString());
			sum = sum.add(amountFen);
		}
		bean.setSum(sum.setScale(0, RoundingMode.UP));
		return bean;
	}

	public List<ProfitDetail> retrieveProfitDetails() {
		return profitDAO.retrieveProfitDetails();
	}

	public ProfitSummary loadProfitSummary(String from, String to) {
		from = from + " 00:00:00";
		to = to + " 23:59:59";
		return profitDAO.loadProfitSummary(from, to);
	}

	public void retrieveProfitDetails(String from, String to,
			PagingBean<ProfitSearchResult> pagingBean) {
		from = from + " 00:00:00";
		to = to + " 23:59:59";
		profitDAO.retrieveProfitDetails(from, to, pagingBean);
	}

	public void searchForPagingList(Member member, String recordType,
			PagingBean<MemberProfitSearchResult> pagingBean) {
		profitDAO.searchForPagingList(member, recordType, pagingBean);
	}

	public double getYesterdaySelledAmount() {
		String date = DateUtils.getYesterdayDateString();
		return profitDAO.retrieveTotalSelledAmount(date + " 00:00:00", date
				+ " 23:59:59");
	}

	public double geTodaySelledAmount() {
		String date = DateUtils.getTodayDateString();
		return profitDAO.retrieveTotalSelledAmount(date + " 00:00:00", date
				+ " 23:59:59");
	}

	public List<ProfitReturnAmountHistory> getReturnHistory(String memberID) {
		return profitDAO.getReturnHistory(memberID);
	}

	public double getSelledAmountByDate(String date) {
		return profitDAO.retrieveTotalSelledAmount(date + " 00:00:00", date
				+ " 23:59:59");
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void returnAllMemberProfit(String countDate, String profitID) {
		List<String> exclusiveMembers = profitDAO
				.getAlreadyReturnedMembersByCountDate(countDate);
		profitDAO.returnProfitForMembers(countDate, exclusiveMembers, profitID);
	}

}
